﻿package core {
    
    import core.object.DropObject;
    import core.object.GameAnimation;
    import core.object.GameBall;
    import core.object.GameProp;
    
    import flash.display.Sprite;
    import flash.events.Event;
	
    public class Shooter extends Sprite {

        private const delay:int = 200;

        private var container:Sprite;
        private var _canShoot:Boolean;
        private var _angle:Number;
		
		public var line:Sprite;
		
        public function Shooter(){
			this.addEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
        }
		
		private var shooteInstance:GameAnimation;
		
        private function addToStageHandler(event:Event):void{
            this.removeEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
			this.container = new Sprite();
			this._canShoot = false;
			
			
			shooteInstance = new GameAnimation(30,false);
			shooteInstance.x = -shooteInstance.width / 2;
			shooteInstance.y = -shooteInstance.height / 2;
			this.container.addChild(shooteInstance);
			this.line = new Sprite();
			this.line.graphics.beginFill(Math.random() * 0xFFFFFF,1);
			this.line.graphics.drawRect(-GameBall.radius >> 1,0,GameBall.radius,Config.LINE_MAX_HEIGHT);
			this.line.graphics.endFill();
			this.line.y = -Config.LINE_MAX_HEIGHT;
			this.addChild(this.line);
			addChild(this.container);
        }
		
		private var lastItem:GameBall;
		
        public function reset():void{
			if(this._item){
				this.item.dispose();
			}
            this._item = null;
        }
		
        public function getStrongBall():void{
        }
        public function getColorfullBall():void{
        }
        public function checkBallColor():void{
        }
        public function clearBalls():void{
        }
		
		private var _item:GameBall;
		
		public function addItem(value:GameBall):void {
			this._item = value.clone() as GameBall;
			this._item.x = -GameBall.radius;
			this.lastItem = this._item;
			this.container.addChildAt(this._item,this.container.numChildren);
		}
		
		public function get item():GameBall {
			return this._item;
		}
		
		public function updateLine(value:Number):void {
			if(isNaN(value)){
				value = Config.LINE_MAX_HEIGHT;
			}
			this.line.graphics.clear();
			this.line.graphics.beginFill(0xFBABAB,1);
			this.line.graphics.drawRect(-GameBall.radius >> 1,0,GameBall.radius,value);
			this.line.graphics.endFill();
			this.line.y = -value;
			if(this._prop && this._prop.id != 9){
				this._prop.y = this.line.y;
			}
		}
		
		public function reduction():void {
			this.addItem(this.lastItem);
		}
		
		private var _prop:GameProp;
		
		private function addProp(value:DropObject):void {
			if(value.isNormal){
				trace("增加分数的道具!!!");
				// 增加分数
			}else {
				if(value.id == 2004){
					this._prop = new GameProp(9,value.id);
					this._prop.x = -GameBall.radius;
					this.container.addChildAt(this._prop,this.container.numChildren);
				}else {
					this._prop = new GameProp(-1,value.id);
					if(value.id == 2001){
						this._prop.graphics.beginFill(0xDDEEAA,0.3);
						this._prop.graphics.drawRect(-GameBall.radius >> 1,0,GameBall.radius,Config.LINE_MAX_HEIGHT);
						this.line.visible = false;
					}else if(value.id == 2002){
						this._prop.graphics.beginFill(0xFF2233,0.3);
						this._prop.graphics.drawCircle(0,0,200);
					}else if(value.id == 2003){
						this._prop.graphics.beginFill(0xAABBCC,0.3);
						this._prop.graphics.drawCircle(0,0,100);
					}
					this._prop.graphics.endFill();
					this.addChild(this._prop);
				}
				
			}
			
		}
		
		public function removeProp():void {
			this._prop.dispose();
			this._prop = null;
			this.line.visible = true;
		}
		
		public function get prop():GameProp {
			return this._prop;
		}
		
		public function catchDrop(value:DropObject):Boolean {
			if(!value){
				return false;
			}
			if(this.shooteInstance.hitTestObject(value)){
				this.addProp(value);
				return true;
			}
			return false;
		}
    }
}
